package cn.sccl.dirms.dev.service.impl;

import cn.sccl.common.database.DataBase;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.common.util.SecurityUtils;
import cn.sccl.dirms.security.model.UserPrincipal;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sccl.common.dao.BaseDAO;
import cn.sccl.dirms.dev.service.GraphsManager;
import cn.sccl.dirms.dev.dao.GraphsDAO;
import cn.sccl.dirms.dev.model.Graphs;

import java.util.HashMap;
import java.util.Map;

/**
 * 图形集信息 的Manager实现类
 * Created by liupan on 2017-06-25.
 */
@Service
public class GraphsManagerImpl extends BaseManagerImpl<Graphs> implements GraphsManager {

    @Autowired
    private GraphsDAO graphsDAO;

    public GraphsDAO getGraphsDAO() {
        return graphsDAO;
    }

    @Override
    public BaseDAO<Graphs> getBaseDAO() {
        return getGraphsDAO();
    }

    @Override
    public  void sendToMenu(Graphs graphs, String displayPath, String parentMenuName,String userId){
        DataBase db = this.getDataBase();
        String sql = "call newMenu(?,?,?,?,?,?)";
        JSONObject menuParams = new JSONObject();
        menuParams.put("grsNo",graphs.getGrsNo());
        menuParams.put("params",graphs.getParams());
        db.execute(sql, new Object[]{graphs.getId(),graphs.getGrsName(), displayPath, menuParams.toJSONString(),parentMenuName, userId});
    }


    @Override
    public void save(Graphs graphs){

        //校验是否存在重复的编号
        if(graphs.getId()==null||"".equals(graphs.getId())){
            Map<String, Object> find = new HashMap<>();
            find.put("grsNo", graphs.getGrsNo());
            Graphs exists = this.findUniqueBy(find);
            if(exists!=null&&exists.getId()!=null){
                throw new  RuntimeException("图形编号："+graphs.getGrsNo()+"的图形已经存在！");
            }
        }
        super.save(graphs);
        DataBase db = this.getDataBase();
        String sql = "call updateMenu(?,?,?,?)";
        JSONObject menuParams = new JSONObject();
        menuParams.put("grsNo",graphs.getGrsNo());
        menuParams.put("params",graphs.getParams());
        db.execute(sql, new Object[]{graphs.getId(),graphs.getGrsNo(),graphs.getGrsName(),menuParams.toJSONString()});
    }


    @Override
    public void delete(String id){
        super.delete(id);
        DataBase db = this.getDataBase();
        String sql = "call deleteMenu(?)";
        db.execute(sql, new Object[]{id});
    }

}